/**
 * Created by smp on 2017/3/16.
 */

//表单提交
$('#dictionarySave').on('click',function () {
    if( $("#ddd").val()==null|| $("#ddd").val()==''){
        layer.msg('请选择字典类型', {
            icon: 2,
            time: 2000
        });
        return;
    }
    if( $(":input[name=code]").val()==null|| $(":input[name=code]").val()==''){
        layer.msg('请填写字典标识', {
            icon: 2,
            time: 2000
        });
        return;
    }
    if( $(":input[name=name]").val()==null|| $(":input[name=name]").val()==''){
        layer.msg('请填写字典名称', {
            icon: 2,
            time: 2000
        });
        return;
    }
    layer.confirm('是否确定保存？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type: "POST",
            url: ROOT_PATH+'/permission/dictionary/addAndAlterDictionaryJudgement.shtml',
            data: {code:$(':input[name=code]').val()},
            dataType: "json",
            success: function(result){
                if (result.success) {
        $.ajax({
            type: "POST",
            url: ROOT_PATH+'/permission/dictionary/addDictionary.shtml',
            data: $("#dictionaryForm").serialize(),
            dataType: "json",
            success: function(result){
                //result = $.parseJSON(result);
                if (result.success){
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:1},function () {
                        parent.$('#dictionary').datagrid('load');
                        parent.layer.closeAll(); //先得到当前iframe层的索引
                    });
                }else {
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:2});
                }
            }
        });
                }
                else {
                    layer.msg('字典标识重复', {
                        icon: 2,
                        time: 2000
                    });
                }
            }
        });
    });

});
//参数清空
$('#dictionaryReset').on('click',function () {
    $('#dictionaryForm').form('reset');
});
$.ajax({
    url:ROOT_PATH + '/permission/dictionaryType/listAll.shtml',
    type:"post",
    dataType:"json",
    success:function(data){
        $('#ccc').combobox({
            data:data,
            valueField:'id',
            textField:'name',
           /* onLoadSuccess: function () { //加载完成后,设置选中第一项
             var val = $(this).combobox("getData");
             for (var item in val[0]) {
             if (item == "id") {
             $(this).combobox("select", val[0][item]);

             }
             }
             },*/
            onSelect:function (newValue,oldValue) {
                $("#ddd").val(newValue.id);
            }
        });
    }
});
    $.extend($.fn.validatebox.defaults.rules, {
        checkCode : {
            validator: function (value) {
                var checkR=$.ajax({
                    async : false,
                    cache : false,
                    type : 'post',
                    url : ROOT_PATH+'/permission/dictionary/addAndAlterDictionaryJudgement.shtml',
                    data : {
                        code:value
                    }
                }).responseText;
                var dataObj=$.parseJSON(checkR);//转换为json对象
                var flag=dataObj.success==true;
                if(!flag){
                    $('#code_err').css('display','block');
                }else {
                    $('#code_err').css('display','none');
                }
                return flag;
            },
            message: '当前标识已存在！'
        }
    });
